'use strict'

// TODO: refactor
// 秘钥必须为：8/16/32位
const AES_SECRET_KEY = 'N2I2NmU3NGZiZTU5MWJiNGJlOTg0N2Y0'

// aes 加密
function encryption(cipher) {
  return CryptoJS.AES.encrypt(
    cipher,
    CryptoJS.enc.Utf8.parse(AES_SECRET_KEY),
    {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    }
  )
}
// aes 解密
function decryption(cipher) {
  const decrypt = CryptoJS.AES.decrypt(
    cipher,
    CryptoJS.enc.Utf8.parse(AES_SECRET_KEY),
    {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    }
  )
  return decrypt.toString(CryptoJS.enc.Utf8)
}

function login() {
  let username = document.querySelector('.fx-username').value
  let password = document.querySelector('.fx-password').value
  username = encryption(username) + ''
  password = encryption(password) + ''
  axios.post('/v1/login', {
      username: username,
      password: password,
    })
    .then(rs => {
      const res = rs.data
      if (res.error) {
        console.error(res.message)
        return
      }
      console.log('login success')
      location.href = '/public/dashboard'
    })
    .catch(err => console.error(err))
}